package com.foreknow.controller;

import com.foreknow.bean.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.MidiFileFormat;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");String m = req.getParameter("q");
        if ("select".equals(m)){
            select(req,resp);
        }else if ("delete".equals(m)){
            delete(req,resp);
        }else if ("update".equals(m)){
            update(req, resp);
        }else if ("insert".equals(m)){
            insert(req,resp);
        }else if ("allright".equals(m)){
            allright(req, resp);
        }else if ("update".equals(m)){
            update(req, resp);
        }else if ("updateright".equals(m)){
            updateright(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");doGet(req,resp);
    }

    /**
     * 查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");
        //向网页响应内容  字符流
        PrintWriter out = resp.getWriter();
        GuestbookService guestbookService = new GuestbookServiceImpl();
        List<Object> list = guestbookService.allquery();
        //如何将list集合保存到一个地方,转发之后可以在list.jsp中方便获取(保存在服务器端)
        req.setAttribute("list",list);
        //当前页面跳转list.jsp页面
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }

    /**
     * 添加
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");
        req.getRequestDispatcher("/AddMessage.jsp").forward(req,resp);

//        req.getRequestDispatcher("/m?q=select").forward(req,resp);

    }

    protected void allright(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");
        String date1 = sdf.format(date);
//        String createdtime = ""+date;
        GuestbookService guestbookService = new GuestbookServiceImpl();
        Guestbook guestbook = new Guestbook();
        guestbook.setName(name);
        guestbook.setPhone(phone);
        guestbook.setEmail(email);
        guestbook.setTitle(title);
        guestbook.setContent(content);
        guestbook.setCreatedtime(date1);
        int a = guestbookService.add(guestbook);
        if (a==1){
            req.getRequestDispatcher("/m?q=select").forward(req,resp);
        }

    }

    /**
     * 修改
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");
        //向网页响应内容  字符流
        PrintWriter out = resp.getWriter();
        GuestbookService guestbookService = new GuestbookServiceImpl();
        String aa = req.getParameter("id");
        int id = Integer.parseInt(aa);
        Guestbook guestbook = guestbookService.query(id);
        req.setAttribute("guest",guestbook);
        req.getRequestDispatcher("/update.jsp").forward(req,resp);
    }


    protected void updateright(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String createdtime = req.getParameter("createdtime");
        Guestbook guestbook = new Guestbook();
        int a = Integer.parseInt(id);
        guestbook.setId(a);
        guestbook.setName(name);
        guestbook.setPhone(phone);
        guestbook.setEmail(email);
        guestbook.setTitle(title);
        guestbook.setContent(content);
        guestbook.setCreatedtime(createdtime);
        GuestbookService guestbookService = new GuestbookServiceImpl();
        int b = guestbookService.update(guestbook);
        if (b==1){
            req.getRequestDispatcher("/m?q=select").forward(req,resp);
        }
    }
    /**
     * 删除
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");
        //向网页响应内容  字符流
        PrintWriter out = resp.getWriter();
        GuestbookService guestbookService = new GuestbookServiceImpl();
        String aa = req.getParameter("id");
        int id = Integer.parseInt(aa);
        int c= guestbookService.delete(id);
        if (c==1){
            req.getRequestDispatcher("/m?q=select").forward(req,resp);
        }
    }
}
